package com.heimali.happyreader;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.heimali.happyreader.db.dao.ArticleDao;
import com.heimali.happyreader.db.dao.ListDao;
import com.heimali.happyreader.model.Article;
import com.heimali.happyreader.utils.ErrorUtils;
import com.heimali.happyreader.utils.FileUtils;
import com.heimali.happyreader.wraper.ArticleWraper;
import com.heimali.happyreader.wraper.BaseWraper;

public class ArticleActivity extends BaseActivity implements OnClickListener {

	public final String TAG = "ArticleActivity";
	private Button rightButton;
	private WebView articleWebView;
	private RelativeLayout titleBarLayout;

	private int articleID;
	private static final String mimeType = "text/html";
	private static final String charset = "utf-8";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		checkLogin();
		initView();
		initProgressBar();
		getDataFromDB();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.right_btn:
			finish();
			break;
		case R.id.progress_bar_retry:
			getDataFromNet();
			break;
		}
	}

	private void initView() {
		articleID = getIntent().getExtras().getInt("articleID");
		setContentView(R.layout.article);
		rightButton = (Button) findViewById(R.id.right_btn);
		rightButton.setText(R.string.back_btn);
		rightButton.setOnClickListener(this);

		titleBarLayout = (RelativeLayout) findViewById(R.id.title_bar);

		articleWebView = (WebView) findViewById(R.id.article);
		WebSettings setting = articleWebView.getSettings();
		setting.setJavaScriptEnabled(true);
		setting.setCacheMode(2);
		// articleWebView.addJavascriptInterface(new Object(){
		// @SuppressWarnings("unused")
		// public void toggleTitleBar() {
		// new Handler().post(new Runnable() {
		// public void run() {
		// toggleTitleBar();
		// }
		// });
		// }
		// }, Constants.JS_NS);
	}

	@SuppressWarnings("unused")
	private void toggleTitleBar() {
		if (titleBarLayout.getVisibility() == View.VISIBLE) {
			titleBarLayout.setVisibility(View.GONE);
		} else {
			titleBarLayout.setVisibility(View.VISIBLE);
		}
	}

	private void getDataFromDB() {
		Article article = new ArticleDao().query(String.valueOf(articleID));
		if (article != null) {
			showArticle(article);
		} else {
			getDataFromNet();
		}
	}

	private void updateDB(final Article article) {
		new Thread(new Runnable() {
			public void run() {
				int count = new ArticleDao().insert(article);
				if (count > 0) {
					new ListDao().updateIsDownload(article.id);
				}
			}
		}).start();
	}

	private void getDataFromNet() {
		progressBarStrat(articleWebView);
		new ArticleWraper(articleID, new BaseWraper.Callback() {

			public void onSuccess(Object result) {
				Article aritcle = (Article) result;
				showArticle(aritcle);
				progressBarStop(articleWebView, true);
				updateDB(aritcle);
			}

			public void onFail(int errno, String errmsg) {
				String msg = (errmsg == null) ? ErrorUtils.getMsg(errno) : errmsg;
				Toast.makeText(ArticleActivity.this, msg, 0).show();
				progressBarStop(articleWebView, false);
			}
		}).execute();

	}

	private void showArticle(Article article) {
		String content = FileUtils.readAssetsFile(ArticleActivity.this, "article.html");
		content = content.replace("{$title}", article.title).replace("{$content}", article.content);
		articleWebView.loadDataWithBaseURL(null, content, mimeType, charset, null);
	}

}
